// 评论管理  API
var express = require('express');
var router = express.Router();
const app = express()
const cors = require('cors');
app.use(cors());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// 日志：
const computingLog = require('../../middlewares/computingLog')
const CommentModel = require('../../Models/Comments');

// 获取数据库数据
router.get('/FindComment', computingLog, function (req, res, next) {
  res.header('Access-Control-Allow-Origin', '*'); // 或指定为 'http://localhost:8080'
  res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
  // 获取数据库数据
  CommentModel.find().sort({ time: -1 }).then((data) => {
    res.json({
      code: '0000',
      msg: '获取成功',
      data: data
    })
  }).catch((err) => {
    res.json({
      code: '1001',
      msg: '获取失败',
      data: []
    })
    return
  })
})

// 评论通过审核
router.post('/PassComment', computingLog, function (req, res, next) {
  res.header('Access-Control-Allow-Origin', '*'); // 或指定为 'http://localhost:8080'
  res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
  // 获取数据库数据
  console.log("收到数据：", req.body);
  let id = req.body.data._id
  console.log(id);

  CommentModel.updateOne({ _id: id }, { state: 1 }).then((err, data) => {
    if (err) {
      res.json({
        code: '2005',
        msg: "审核错误",
        data: []
      })
    } else {
      res.json({
        code: '0000',
        msg: "审核通过",
        data: data
      })
    }
  })
})

// 评论不通过审核
router.post('/NoPassComment', computingLog, function (req, res, next) {
  res.header('Access-Control-Allow-Origin', '*'); // 或指定为 'http://localhost:8080'
  res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
  // 获取数据库数据
  // console.log("收到数据：", req.body);
  let id = req.body.data._id
  console.log(id);

  CommentModel.updateOne({ _id: id }, { state: 2 }).then((err, data) => {
    if (err) {
      res.json({
        code: '2005',
        msg: "操作失败",
        data: []
      })
    } else {
      res.json({
        code: '0000',
        msg: "操作成功",
        data: data
      })
    }
  })
})

// 删除一条评论数据
router.delete('/DeleComment/:id', computingLog, function (req, res) {
  res.header('Access-Control-Allow-Origin', '*'); // 或指定为 'http://localhost:8080'
  res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
  let id = req.params.id
  CommentModel.deleteOne({ _id: id }).then((data) => {
    res.json({
      code: '0000',
      msg: '删除成功',
      data: ''
    })
  }).catch((err) => {
    res.json({
      code: '1002',
      msg: '删除失败',
      data: null
    })
    return
  })
})


// 添加数据库评论数据
router.post('/AddComment', computingLog, function (req, res, next) {
  res.header('Access-Control-Allow-Origin', '*'); // 或指定为 'http://localhost:8080'
  res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
  // 获取数据库数据
  // console.log("收到数据：", req.body);
  CommentModel.create({ ...req.body }).then((data) => {
    res.json({
      code: '0000',
      msg: '添加成功',
      data: data
    })
  }).catch((err) => {
    res.json({
      code: '1003',
      msg: '添加失败',
      data: null
    })
    return
  })
})









module.exports = router;